using ABA;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace FW
{
    public class ConvertExcelWind : EditorWindow
    {
        private List<SettingItem> settingItemList;
        private UnityEngine.Vector2 scrollPos;
        private GUIStyle titleStyle;
        private GUIStyle infoStyle;



        [MenuItem("FrameWork/Excel转表工具",false,1)]
        public static void ConvertExcelConfig()
        {
            GetWindow<ConvertExcelWind>(false, "转换配表设置", true);
        }

        public void Awake()
        {
            titleStyle = new GUIStyle();
            titleStyle.normal.textColor = Color.red;
            titleStyle.fontStyle = FontStyle.Bold;
            titleStyle.fontSize = 15;

            infoStyle = new GUIStyle();
            infoStyle.fontSize = 14;
            infoStyle.normal.textColor = Color.white;
        }


        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal(); //起一行
            if (GUILayout.Button("刷新目录", GUILayout.Width(90)))
            {
                Init();
            }

            if (GUILayout.Button("打开设置", GUILayout.Width(90)))
            {
                SettingWind.ShowWindow(Path_ExcelSetting.TAG);
            }
            
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("选择", titleStyle, GUILayout.Width(40)); //标题
            GUILayout.Label("配表名称", titleStyle, GUILayout.Width(249));
            GUILayout.EndHorizontal();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
            if (settingItemList == null)
                Init();

            for(int i = 0; i < settingItemList.Count; i++)
            {
                SettingItem item = settingItemList[i];
                GUILayout.BeginHorizontal();
                item.Selected = GUILayout.Toggle(item.Selected, "", GUILayout.Width(40));
                GUILayout.Label(item.name, infoStyle, GUILayout.Width(249));
                GUILayout.EndHorizontal();
            }

            EditorGUILayout.EndScrollView();
            
            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("生成选中", GUILayout.Height(50)))
            {
                var list = new List<SettingItem>();
                foreach (var item in settingItemList)
                {
                    if (item.Selected) list.Add(item);
                }
                if (list.Count > 0)
                    ConverConfig.GenCSharp(list);
                else
                    EditorUtility.DisplayDialog("提示", "至少勾选一项", "好吧");
                //转换选中
            }

            if (GUILayout.Button("导入选中数据", GUILayout.Height(50)))
            {
                var selectList = new List<SettingItem>();
                settingItemList.ForEach((item) =>
                {
                    if(item.Selected) selectList.Add(item);
                }
                );

                if (settingItemList.Count > 0)
                {
                    ImportData(selectList);
                }
                else
                {
                    EditorUtility.DisplayDialog("提示", "请至少勾选一项才能导入数据", "好的");
                }

            }


            if (GUILayout.Button("生成全部",GUILayout.Height(50)))
            {
                if (settingItemList.Count > 0)
                    ConverConfig.GenCSharp(settingItemList);
                AssetDatabase.Refresh();
            }

            if (GUILayout.Button("导入全部", GUILayout.Height(50)))
            {
                ImportData(settingItemList);
            }
            EditorGUILayout.EndHorizontal();


        }

        void ImportData(List<SettingItem> datas)
        {


        }
        void Init()
        {
            settingItemList =  new List<SettingItem>();
            if (Directory.Exists(Path_ExcelSetting.ExcelFolder))
            {
                DirectoryInfo folderInfo = new DirectoryInfo(Path_ExcelSetting.ExcelFolder);
                FileInfo[] fileInfoArr = folderInfo.GetFiles("*.xlsx");
                foreach (FileInfo fileInfo in fileInfoArr)
                {
                    if (fileInfo.Name.StartsWith("~$")) continue;//过滤 临时已打开的excel表格
                    var item = new SettingItem(Path_ExcelSetting.ExcelFolder, fileInfo.Name);
                    settingItemList.Add(item);  
                }

            }
        }

    }



    public class SettingItem
    {
        public string name;
        public string folder;
        public bool Selected = false;

        public SettingItem(string folder, string name)
        {
            if (string.IsNullOrEmpty(folder))
                throw new ArgumentException("folder is null");
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name is null");

            this.folder = folder;
            this.name = name;
        }


    }
}